#include <Ray.hpp>

glm::vec3 Ray::At(float t) const { return mOrigin + t * mDirection; }

glm::vec3 Ray::RayColor(const Ray &ray) {
  auto unit_direction = glm::normalize(ray.Direction());
  auto t = 0.5f * (unit_direction.y + 1.f);

  return (1.f - t) * glm::vec3(1.f, 1.f, 1.f) + t * glm::vec3(0.5f, 0.7f, 1.f);
}

glm::vec3 Ray::Refract(const glm::vec3 &uv, const glm::vec3 &n,
                       float etai_over_etat) {
  float cos_theta = glm::min(glm::dot(-uv, n), 1.f);

  glm::vec3 r_out_perp = etai_over_etat * (uv + cos_theta * n);
  float out_perp_length_squared = glm::length(r_out_perp);
  out_perp_length_squared *= out_perp_length_squared;

  glm::vec3 r_out_parallel =
      -glm::sqrt(glm::abs(1.f - out_perp_length_squared)) * n;

  return r_out_perp + r_out_parallel;
}
